We want to use the new service events properly by
modifying the current events. This will allow us to reuse these
structured literals quickly.
Modifying Our <OnStart>
We will be modifying our current tutorials.vb
thread function code to reflect both the changes in using our resource
file and to use our new InstanceId constants.
Listing 1. Modifications to <OnStart> to support the new resource file.
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set things
' in motion so your service can do its work.
Try
m_WorkerThread = New Thread(AddressOf ThreadFunc)
m_WorkerThread.Name = My.Resources.ThreadName
m_WorkerThread.Priority = ThreadPriority.Normal
m_WorkerThread.Start()
WriteLogEvent(My.Resources.ServiceStarting, ONSTART_INFO, _
EventLogEntryType.Information, My.Resources.Source)
Catch ex As Exception
'We Catch the Exception
'to avoid any unhandled errors
'and we will stop the service if any occur here
Me.Stop()
End Try
End Sub
|
The listing shows that I am now using the My Object to read from the localization resource file.
Modifying <OnStop>
Now let’s update the <OnStop> method to reflect the use of Resources and WriteEventLog call changes. Listing 2 shows the changes.
Listing 2. Modifications to <OnStop> to support the new resource file.
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your service.
Try
If (Not m_WorkerThread Is Nothing) Then
Try
Me.RequestAdditionalTime(THIRTY_SECONDS)
m_WorkerThread.Join(TIME_OUT)
m_ThreadAction.StopThread = True
Catch ex As Exception
m_ThreadAction = Nothing
End Try
End If
WriteLogEvent(My.Resources.ServiceStopping, ONSTOP_INFO, _
EventLogEntryType.Information, My.Resources.Source)
Catch ex As Exception
End Try
End Sub
|
Modifying <OnPause>
As shown in Listing 3, you must once again update <OnPause>.
Listing 3. Modifications to <OnPause> to support the new resource file.
Protected Overrides Sub OnPause()
Try
m_ThreadAction.Pause = True
WriteLogEvent(My.Resources.ServicePausing, ONPAUSE_INFO, _
EventLogEntryType.Information, My.Resources.Source)
Catch ex As Exception
'We Catch the Exception
'to avoid any unhandled errors
'since we are pausing and
'logging an event is what failed
'we will merely write the output
'to the debug window
Debug.WriteLine("Error pausing service: " + ex.ToString())
Me.Stop()
'do nothing
End Try
End Sub
|
Modifying <OnContinue>
Listing 4 shows how to update the <OnContinue> method.
Listing 4. Modifications to <OnContinue> to support the new resource file.
Protected Overrides Sub OnContinue()
Try
m_ThreadAction.Pause = False
WriteLogEvent(My.Resources.ServiceContinuing, ONCONTINUE_INFO, _
EventLogEntryType.Information, My.Resources.Source)
Catch ex As Exception
'We Catch the Exception
'to avoid any unhandled errors
'since we are resuming and
'logging an event is what failed
'we will merely write the output
'to the debug window
Debug.WriteLine("Error resuming service: " + ex.ToString())
Me.Stop()
End Try
End Sub
|